﻿using System;
using System.Collections.Generic;
using System.Text;
using ApiCoreProject.Model.Entities;
using ApiCoreProject.Bll.Interface;
using ApiCoreProject.Dal;
using System.Linq;
using System.Linq.Expressions;
using System.Dynamic;

namespace ApiCoreProject.Bll.Implement
{
    public class UsersServe : BaseBll<User>, IUsersServe
    {
        public UsersServe(IBaseDal<User> dal) : base(dal)
        {
        }
        public User Login(string loginName, string loginPwd)
        {
            var models = dal.GetModels(u => u.LoginName.Equals(loginName) && u.LoginPwd.Equals(loginPwd));

            var user = (from u in models
                       select new User
                       {
                           UserId = u.UserId,
                           Name = u.Name,
                           LoginName = u.LoginName,
                           Sex = u.Sex,
                           Phone =u.Phone,
                           Status = u.Status,
                           RoleId = u.RoleId,
                           Role = new Role { 
                               RoleId = u.Role.RoleId,
                               RoleName = u.Role.RoleName
                           },
                           ImgId = u.ImgId,
                           Img = new Image {
                               ImgId = u.Img.ImgId,
                               Path = u.Img.Path
                           }
                       }).FirstOrDefault();

            return user;
        }

        public dynamic GetUsers(int pageSize, int pageIndex, string name)
        {
            var users = dal.GetModels(u=>string.IsNullOrWhiteSpace(name)?true:u.Name.Contains(name));
            var lists = dal.GetModelsByPage<int>(pageSize, pageIndex, true, u => u.UserId, uu => string.IsNullOrWhiteSpace(name) ? true : uu.Name.Contains(name));

            var list = from u in lists
                       select new {
                           u.UserId,
                           u.Name,
                           u.Sex,
                           u.Phone,
                           u.LoginName,
                           u.ImgId,
                           u.Img.Path,
                           u.Status,
                           u.LastTime,
                           u.RoleId,
                           u.Role.RoleName,
                           u.Role.DeptId,
                           u.Role.Dept.DeptName
                       };

            dynamic d = new ExpandoObject();
            d.size = pageSize;
            d.page = pageIndex;
            d.count = users.Count();
            d.list = list.ToList();

            return d;
        }

        public dynamic AddUsers(User user)
        {
            dynamic d = new ExpandoObject();
            user.LoginPwd = md5(user.LoginPwd); //加密密码
            var u = dal.Add(user);
            if (dal.SaveChanges()>0)
            {
                d.code = 1;
                d.msg = "添加成功！";
            }
            else {
                d.code = -1;
                d.msg = "添加失败！";
            }
            return d;
        }

        public dynamic DeleteUsers(int userID)
        {
            dynamic d = new ExpandoObject();
            var user = dal.GetModels(u => u.UserId == userID).FirstOrDefault();
            if (user == null)
            {
                d.code = -1;
                d.msg = "没有此数据记录，或者已经被删除！";
            }
            else {
                dal.Delete(user);
                int code = dal.SaveChanges();
                d.code = code;
                d.msg = code > 0 ? "删除成功！" : "删除失败！";
            }
            return d;
        }

        public dynamic DeleteBatch(int[] ids)
        {
            dynamic d = new ExpandoObject();
            var reusers = dal.GetModels(u => ids.Contains(u.UserId)).ToList();
            if (reusers.Count > 0)
            {
                dal.DeleteBatch(reusers);
                int code = dal.SaveChanges();
                d.code = code;
                d.msg = code > 0 ? "删除成功！" : "删除失败！";
            }
            else {
                d.code = -1;
                d.msg = "没有此数据记录，或者已经被删除！";
            }

            return d;
        }

        public dynamic UpdateUsers(int id,User user)
        {
            dynamic d = new ExpandoObject();
            var users = dal.GetModels(u => u.UserId == id).FirstOrDefault();

            if (users == null)
            {
                d.code = -1;
                d.msg = "没有此数据记录，或者已经被删除！";
            }
            else {
                users.Name = user.Name;
                users.Sex = user.Sex;
                users.Phone = user.Phone;
                users.Status = user.Status;
                users.ImgId = user.ImgId;
                users.RoleId = user.RoleId;
                users.LastTime = DateTime.Now;
                dal.Update(users);
                int code = dal.SaveChanges();
                d.code = code;
                d.msg = code > 0 ? "修改成功！" : "修改失败！";
            }
            return d;
        }


        public string md5(string pwd)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd))).Replace("-", null);//加密输入进来的密码
        }
    }
}
